//
//  LXSearchTableViewController.m
//  cat
//
//  Created by imera on 16/1/20.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXSearchTableViewController.h"
#import "LXSearchBarView.h"
#import "LXSearchTableViewCell.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#import "LXVideoShowViewController.h"

@interface LXSearchTableViewController() <LXSearchBarViewDelegate,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic, weak) LXSearchBarView *searchBarView;

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, strong) NSArray *videosArr;

@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation LXSearchTableViewController

- (void)viewDidLoad
{
    [self initNavBar];
    
    [self initTableView];

    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)initTableView
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 60)];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

- (void)initNavBar
{
    LXSearchBarView *navBarView = [[LXSearchBarView alloc]init];
    navBarView.backgroundColor = MainBackgroundBlueColor;
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, 40);
    navBarView.MyDelagate = self;
    [self.view addSubview:navBarView];
    self.searchBarView = navBarView;
    
    navBarView.textField.delegate = self;
}

#pragma mark LXSearchBarView的代理
- (void)popSelfController:(LXSearchBarView *)searchBarView
{
   [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchBarView:(LXSearchBarView *)searchBarView searchWithField:(NSString *)field
{
    NSString *searchKey = self.searchBarView.textField.text;

    if (![searchKey isEqualToString:@""]) {
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

        NSString *url = @"http://cat666.com/cat666-interface/index.php/index/search";
        [mgr POST:url parameters:@{@"key":searchKey} success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSArray *videos = responseObject;
            NSDictionary *item = videos[0];
            if (item.allKeys.count == 1) { //没有搜索到相关的视频
                [MBProgressHUD showError:@"没有搜索到相关的视频"];
            }else{
                NSMutableArray *videosArr = [NSMutableArray array];
                for (NSDictionary *dict in videos) {
                    LXVideo *video = [LXVideo initWithDict:dict];
                    [videosArr addObject:video];
                }
                self.videosArr = videosArr;
                //刷新表格
                [self.tableView reloadData];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [MBProgressHUD showError:@"您的网络开小差了"];
        }];
    }else {
        [MBProgressHUD showError:@"请输入要搜索的名字"];
    }
}


#pragma mark tableView的代理和协议
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.videosArr.count;
}

- (LXSearchTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"Cell";
    
    LXSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[LXSearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    if (self.videosArr[indexPath.row] != nil) {
        cell.video = self.videosArr[indexPath.row];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LXVideoShowViewController *movieShowVc = [[LXVideoShowViewController alloc]init];
    movieShowVc.video = self.videosArr[indexPath.row];
    [self.navigationController pushViewController:movieShowVc animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 键盘的代理事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
@end
